--衍生技 無界無終
local wujiewuzhong = fk.CreateSkill {
    name = "mcdh_wujiewuzhong",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

wujiewuzhong:addEffect(fk.TurnEnd,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and not player:insideExtraTurn() and target == player then
            if type(player:getMark("@wujiewuzhong-turn")) == "table" and #player:getMark("@wujiewuzhong-turn") == 4 then
                return true
            else
                local events =  player.room.logic:getEventsOfScope(GameEvent.MoveCards,999, function(e)
                    for _, move in ipairs(e.data) do
                        if move.toArea == Card.DiscardPile then
                            return true
                        end
                    end
                end, Player.HistoryTurn)
                local suits = {}
                for _, e in ipairs(events) do
                    for _, move in ipairs(e.data) do
                        for _, info in ipairs(move.moveInfo) do
                            if not table.contains(suits,Fk:getCardById(info.cardId).suit ) then
                                table.insert(suits, Fk:getCardById(info.cardId).suit)
                                if #suits == 4 then
                                    return true
                                end
                            end
                        end
                    end
                end
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        player:gainAnExtraTurn(true)
    end,
})

wujiewuzhong:addEffect(fk.TurnStart,{
    can_refresh = function(self, event, target, player, data)
        return player == target and not player:insideExtraTurn()
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        if player:hasSkill(self, true) then
            player.room:setPlayerMark(player, "@wujiewuzhong-turn", {})
        elseif player:usedSkillTimes(self.name, Player.HistoryRound) > 0 then
            player:setSkillUseHistory(self.name, 0, Player.HistoryRound)
        end

    end
})

wujiewuzhong:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        if player.room.current == player then
            local mark = player:getMark("@wujiewuzhong-turn")
            if type(mark) == "table" then
                return #mark < 4
            else
                return player:hasSkill(self, true)
            end
        end
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local suitsRecorded = player:getTableMark( "@wujiewuzhong-turn")
        local mark_change = false
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                for _, info in ipairs(move.moveInfo) do
                    local suit = Fk:getCardById(info.cardId):getSuitString(true)
                    if not table.contains(suitsRecorded, suit) then
                        mark_change = true
                        table.insert(suitsRecorded, suit)
                    end
                end
            end
        end
        if mark_change then
            room:setPlayerMark(player, "@wujiewuzhong-turn", suitsRecorded)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_wujiewuzhong"] = "無界無終",
    [":mcdh_wujiewuzhong"] = "你的非额外回合结束时，若本回合置入弃牌堆的牌中包含四种花色，你可以执行一个额外的回合。",
    ["@wujiewuzhong-turn"] = "無界無終",
}

return wujiewuzhong